package com.example.clock;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class DataListViewAdapter extends BaseAdapter {
    private Context context;
    private List<DataInfoBean> list;
    private OnItemLongClickListener listener;
    public DataListViewAdapter(Context context, List<DataInfoBean> list) {
        this.context = context;
        this.list = list;
    }

    public void setList(List<DataInfoBean> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }

    public void setListener(OnItemLongClickListener listener) {
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.data_list_item,parent,false);
            holder.department = convertView.findViewById(R.id.department);
            holder.workTime = convertView.findViewById(R.id.workTime);
            holder.time = convertView.findViewById(R.id.time);
            holder.day = convertView.findViewById(R.id.day);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.department.setText(list.get(position).getDepartment()+"");
        holder.workTime.setText(list.get(position).getWorkTime()+"");
        holder.day.setText(list.get(position).getDay()+"");
        holder.time.setText(list.get(position).getYear()+"/"+list.get(position).getMonth()+"/"+list.get(position).getDay());
        convertView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (listener!=null){
                    listener.onItemLongClicked(list.get(position).getYear(),list.get(position).getMonth(),list.get(position).getDay());
                }
                return false;
            }
        });
        return convertView;
    }

    class ViewHolder {
        private TextView department;
        private TextView workTime;
        private TextView time;
        private TextView day;
    }

    public interface OnItemLongClickListener{
        void onItemLongClicked(int year , int month , int day);
    }
}
